package com.zan.zanchat.common.user.service.impl;

import com.zan.zanchat.common.user.domain.enums.RoleEnum;
import com.zan.zanchat.common.user.service.IRoleService;
import com.zan.zanchat.common.user.service.cache.UserCache;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @Author Zan
 * @Create 2024/3/18 13:55
 * @Description :
 */
@Service
public class RoleServiceImpl implements IRoleService {

    @Resource
    private UserCache userCache;

    /**
     * 判断是否有某个权限
     * 注意：如果是Admin超级管理员，即所有权限都有
     * @param roleEnum 传入的某个权限枚举，判断是否有该权限
     */
    @Override
    public boolean hasPower(Long uid, RoleEnum roleEnum) {
        // 获得该用户的所有权限ID，roleId
        Set<Long> roleSet = userCache.getRoleSetByUid(uid);
        return isAdmin(roleSet) || roleSet.contains(roleEnum.getId());
    }

    /**
     * 判断是否为超级管理员
     */
    private boolean isAdmin(Set<Long> roleSet) {
        return roleSet.contains(RoleEnum.ADMIN.getId());
    }
}
